﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TheHeGeoDao;

namespace TheHeGeoManager.Util
{
    public class SensorAlert
    {
        private int _thoiGianLayMauBaoCao;
        public string MaSensor { get; set; }
        public string TenSensor { get; set; }
        public string MaThietBi { get; set; }
        public int MaDuAn { get; set; }
        public List<NguoiSuDung> DsNguoiGui { get; set; }
        public string TenThietBi { get; set; }
        public string TenDuAn { get; set; }
        protected double SensorValues { get;  set; }
        public double LastValue { get; set; }
        protected int NumOfSample { get; set; }
        public double GioiHanCanhBao { get; set; }
        public int ThoiGianLayMauCanhBao {
            get { return _thoiGianLayMauBaoCao; }
            set
            {
                _thoiGianLayMauBaoCao = value;
                AlertTime = DateTime.Now.AddSeconds(_thoiGianLayMauBaoCao);
            }
        }
        public DateTime AlertTime { get; protected set; }

        public SensorAlert()
        {
            GioiHanCanhBao = 0;
            ThoiGianLayMauCanhBao = 0;
            Reset();
        }

        public void Reset()
        {
            SensorValues = 0;
            NumOfSample = 0;
            AlertTime = DateTime.Now.AddSeconds(_thoiGianLayMauBaoCao);
        }

        public void SetValue(double val)
        {
            if(GioiHanCanhBao.Equals(0)||ThoiGianLayMauCanhBao.Equals(0)) return;
            SensorValues += val;
            NumOfSample ++;
        }

        public bool Alert()
        {
            if (NumOfSample == 0 || _thoiGianLayMauBaoCao==0) return false;
            var val = SensorValues/NumOfSample;
            bool rs =  val> GioiHanCanhBao&&AlertTime<DateTime.Now&&LastValue<GioiHanCanhBao;
            LastValue = val;
            Reset();
            return rs;
        }
    }
}
